package cc.admin.modules.graduate.service.impl;

import cc.admin.modules.graduate.entity.DesignEnglishQuestions;
import cc.admin.modules.graduate.entity.DesignEnglishQuestionsList;
import cc.admin.modules.graduate.mapper.DesignEnglishQuestionsListMapper;
import cc.admin.modules.graduate.mapper.DesignEnglishQuestionsMapper;
import cc.admin.modules.graduate.service.IDesignEnglishQuestionsListService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import me.zhyd.oauth.utils.UuidUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @Description: 算法设计与分析题主表
 * @Author: cc-admin
 * @Date:   2025-02-20
 * @Version: V1.0.0
 */
@Service
public class DesignEnglishQuestionsListServiceImpl extends ServiceImpl<DesignEnglishQuestionsListMapper, DesignEnglishQuestionsList> implements IDesignEnglishQuestionsListService {


    @Autowired
    private DesignEnglishQuestionsMapper designEnglishQuestionsMapper;

    @Override
    public String saveChildDesign(DesignEnglishQuestionsList designEnglishQuestionsList,String parentId) {

        ArrayList<String> childIds = new ArrayList<>();
        designEnglishQuestionsList.getQuestionList().forEach(question -> {
            String childId = UuidUtils.getUUID();
            question.setId(childId);
            question.setParentId(parentId);
            childIds.add(childId);
            designEnglishQuestionsMapper.insert(question);
        });
        String childIdsString = StringUtils.join(childIds, ",");
        return childIdsString;
    }

    @Override
    public List<DesignEnglishQuestions> getchildDesign(List<String> childIds) {

        List<DesignEnglishQuestions> designEnglishQuestions = designEnglishQuestionsMapper.selectBatchIds(childIds);
        return designEnglishQuestions;
    }

    @Override
    public String updateChildDesign(DesignEnglishQuestionsList designEnglishQuestionsList) {
        LambdaQueryWrapper<DesignEnglishQuestions> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DesignEnglishQuestions::getParentId, designEnglishQuestionsList.getId());
        designEnglishQuestionsMapper.delete(queryWrapper);
        String s = this.saveChildDesign(designEnglishQuestionsList, designEnglishQuestionsList.getId());
        return s;
    }
}
